<template>
	<!-- 赋值权限 -->
	<v-dialog v-model="setActionModel.setActionDialog" width="1400">
		<v-card>
			<v-card-title>赋值权限</v-card-title>
			<v-card-text>
				<v-container>
					<v-select v-model="actionType" :items="RequestEquipmentItems" label="选择分配权限类型" outlined
						@input="selectActionTypeEvent">
					</v-select>
					<v-row>
						<v-col>
							<v-alert text color="blue-grey">所有菜单</v-alert>
							<v-treeview selectable item-disabled="locked" v-model="selection" :items="items"
								item-key="id" return-object open-all dense>
							</v-treeview>
						</v-col>
						<v-divider vertical></v-divider>
						<v-col cols="6">
							<v-alert text color="blue-grey">选中的权限</v-alert>
							<template v-if="!selection.length">
								选中左侧赋值权限
							</template>
							<template v-else>
								<div v-for="node in selection" :key="node.id">
									<a v-if="node.isbtn"
										style="color: #67C23A;">{{ node.parentname+'--'+node.name }}</a>
									<a v-else-if="node.ismenu"
										style="color:blue">{{ node.parentname +'--'+node.name }}</a>
									<a v-else-if="node.isaction"
										style="color:brown">{{ node.parentname+'--'+node.name }}</a>
									<a v-else style="color:darkgreen">{{ node.parentname+'--'+node.name }}</a>
								</div>
							</template>
						</v-col>
					</v-row>
				</v-container>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="success" class="mr-4" :disabled="selection.length<=0" @click="setActionSubmit">确认
					</v-btn>
					<v-btn color="warning" class="mr-4" @click="close">取消</v-btn>

				</v-card-actions>
			</v-card-text>
		</v-card>
	</v-dialog>
</template>

<script>
	import {
		mapActions,
		mapState
	} from 'vuex'
	export default {
		name: 'set_action',
		props: {
			setActionModel: {
				setActionDialog: false,
				roleGroupId: 0,
				roleId: 0
			},
		},
		watch: {
			setActionModel: {
				handler(newVal) {
					this.$emit('update:setActionModel', newVal)

					this.loadAllMenus()
				},
				deep: true
			},
		},
		data: () => ({
			selection: [],
			items: [],
			RequestEquipmentItems: [],
			actionType: 0
		}),

		methods: {
			...mapActions('mes/common', [
				//获取物料信息
				'LoadTextSameValueTypes'
			]),
			async loadRequestEquipment() {
				this.RequestEquipmentItems = await this.LoadTextSameValueTypes({
					pramterEnums: ['RequestEquipment']
				})
			},

			async loadAllMenus() {
				if (!this.setActionModel.setActionDialog) {
					return
				}
				this.loadRequestEquipment()
				this.selection = []
				if (this.setActionModel.roleGroupId > 0) {
					await this.loadRoleGroupActionsMenus()
				} else if (this.setActionModel.roleId > 0) {
					await this.loadRoleActionsMenus()
				}
			},

			async loadRoleGroupActionsMenus() {
				var response = await this.$httpRequest({
					url: `SPM/Role/LoadRoleGroupActionListByGroupId?roleGroupId=${this.setActionModel.roleGroupId}&site=${this.actionType}`,
					method: 'get'
				})

				if (response.status == 1) {
					this.items = response.data.menus
					this.selection = response.data.selectMenus
				} else {
					this.$message.error(response.info)
				}
			},
			async loadRoleActionsMenus() {
				var response = await this.$httpRequest({
					url: `SPM/Role/LoadRoleActionListByRoleId?roleId=${this.setActionModel.roleId}&site=${this.actionType}`,
					method: 'get'
				})

				if (response.status == 1) {
					this.items = response.data.menus
					this.selection = response.data.selectMenus
				} else {
					this.$message.error(response.info)
				}
			},
			//选择权限类型事件
			async selectActionTypeEvent(item) {
				this.actionType = item
				if (this.setActionModel.roleGroupId > 0) {
					await this.loadRoleGroupActionsMenus()
				} else if (this.setActionModel.roleId > 0) {
					await this.loadRoleActionsMenus()
				}
			},
			async setActionSubmit() {
				var actionIdList = this.selection.map(r => {
					return r.id
				})
				if (this.setActionModel.roleGroupId > 0) {
					await this.roleGroupActionsSubmit(actionIdList)
				} else if (this.setActionModel.roleId > 0) {
					await this.roleActionsSubmit(actionIdList)
				}
			},

			async roleGroupActionsSubmit(actionIdList) {
				var response = await this.$httpRequest({
					url: `SPM/Role/SetRoleGroupAction/${this.setActionModel.roleGroupId}`,
					method: 'post',
					data: actionIdList
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.close()
				} else {
					this.$message.error(response.info)
				}
			},
			async roleActionsSubmit(actionIdList) {
				var response = await this.$httpRequest({
					url: `SPM/Role/SetRoleActions/${this.setActionModel.roleId}`,
					method: 'post',
					data: actionIdList
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.close()
				} else {
					this.$message.error(response.info)
				}
			},
			close() {
				this.selection = []
				this.setActionModel.setActionDialog = false
				this.setActionModel.roleGroupId = 0
				this.setActionModel.roleId = 0
			},
		}
	}
</script>

<style>
</style>
